********************************************************************************
* MANAGE LISA RAW DATA                            
* File description: Cleaning raw delivery from SCB from the LISA registry
* Deflate all income variables to 2016 and harmonizing registers between years
********************************************************************************


clear all
set more off

*****CPI******

local	cpi2017	0.982366272
local	cpi2016	1
local	cpi2015	1.00982926440083
local	cpi2014	1.00937828957861
local	cpi2013	1.00754632872699
local	cpi2012	1.00709739019733
local	cpi2011	1.0160549722249
local	cpi2010	1.04274039412114
local	cpi2009	1.05596342521524
local	cpi2008	1.05262632646951
local	cpi2007	1.08922240198272
local	cpi2006	1.11332770389135
local	cpi2005	1.12849500713267
local	cpi2004	1.1333452722063
local	cpi2003	1.13782811938152

forval i=2003(1)2017 { 

use "raw/LISA/lisa_`i'.dta", clear

gen year=`i' 

*Gen variable containing number of kids living at home

if year<=2004 {
gen kids=Barn0_3+Barn4_6+Barn7_10+Barn11_15+Barn16_17+Barn18plus
drop Barn0_3 Barn4_6 Barn7_10 Barn11_15 Barn16_17 Barn18plus
}	

if year>=2005 {
gen kids=Barn0_3+Barn4_6+Barn7_10+Barn11_15+Barn16_17+Barn18_19+Barn20plus
drop Barn0_3 Barn4_6 Barn7_10 Barn11_15 Barn16_17 Barn18_19 Barn20plus
}

*Indicator for being married

gen married=0
replace married=1 if Civil=="G"
label define married 1 "married" 0 "unmarried" 
drop Civil

*Harmonize income variables: 
*dispinc: disposable income
*dispinch: household disposable income
if year<=2003 {
	
gen dispinc=DispInk*`kpi`i''
label var dispinc "CPI 2016 adjusted disposible income"
drop DispInk

gen dispinch=DispInkFam*`kpi`i''
label var dispinch "CPI 2016 adjusted household disposible income"
drop DispInkFam	
	
}

if year>=2004 & year<=2015 {
gen dispinc=DispInk04*`kpi`i''
label var dispinc "CPI 2016 adjusted disposible income"
drop DispInk04

gen dispinch=DispInkFam04*`kpi`i''
label var dispinch "CPI 2016 adjusted household disposible income"
drop DispInkFam04

}

if year>=2016  {
destring Dispink04, replace
destring DispinkFam04, replace
gen dispinc=Dispink04*`kpi`i''
label var dispinc "CPI 2016 adjusted disposible income"
drop Dispink04

gen dispinch=DispinkFam04*`kpi`i''
label var dispinch "CPI 2016 adjusted household disposible income"
drop DispinkFam04

*Social security number
rename Lopnr_PersonNr LopNr
}
*Raw data delivered in 100SEK
replace dispinc=dispinc*100
replace dispinch=dispinch*100



rename ArbLosTyp unemployed
destring unemployed, replace

rename Alder age
destring age, replace

label var AstNr "ID for work place"

rename LopNr_CfarNr CfarNr

label var CfarNr "ID for work place"

label var LopNr "ID for individual"

*Education 
rename Sun2000niva_old educ_old
replace educ_old="." if educ_old=="*"
destring educ_old, replace
label define educ_old 1 "<9 years" 2 "9 years" 3 "Some high-school <3y" 4 "High-school 3y" 5 "Some college <3y" 6 "College >3y" 7 "PhD"
gen educ=0
replace educ=8 if educ_old==1
replace educ=9 if educ_old==2
replace educ=11 if educ_old==3
replace educ=12 if educ_old==4
replace educ=14 if educ_old==5
replace educ=15 if educ_old==6
replace educ=20 if educ_old==7
replace educ=. if educ_old==.

if year>=2016 {
destring Sun2000niva, replace
replace educ=8 if Sun2000niva==100 | Sun2000niva==106	
replace educ=9 if Sun2000niva==200 | Sun2000niva==204| Sun2000niva==206	
replace educ=11 if Sun2000niva=>300 & Sun2000niva<=327
replace educ=12 if Sun2000niva=>330 & Sun2000niva<=337
replace educ=14 if Sun2000niva=>410 & Sun2000niva<=527
replace educ=15 if Sun2000niva=>530 & Sun2000niva<=557
replace educ=20 if Sun2000niva=>600 & Sun2000niva<=640
replace educ=. if mi(Su2000niva)
	
}

rename Kommun municipality
destring municipality, replace
replace municipality=331 if municipality==1917 /*changed name of muni*/
destring Lan, replace
rename Lan district 

destring YrkStalln, replace
gen employed=0
replace employed=1 if YrkStalln==1 | YrkStalln==2

gen selfemployed=0
replace selfemployed=1 if YrkStalln==3 |YrkStalln==5 |YrkStalln==4
drop YrkStalln

destring Kon, replace
gen female=0
replace female=1 if Kon==2
drop Kon
label define female 1 "female" 0 "male"

destring ForTid SjukTyp SocBidrTypF, replace
gen early_retirement=0 
replace early_retirement=1 if ForTid>0

rename SjukTyp sick_leave
rename SocBidrTypF soc_ben

keep LopNr year age dispinc dispinch kids married unemployed educ female municipality district selfemployed CfarNr early_retirement sick_leave soc_ben

*Create income percentiles at the municipality level

bys municipality: egen percentile=xtile(dispinc), nq(10)

save "use/LISA/Lisa_`i'.dta", replace

}



